package com.czy.owner.ui.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.global.Constants;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.widget.PageTitleView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2017/6/24 0024.
 * 返回支付成功页面
 */

public class BackPaidSuccessActivity extends BaseActivity {
    private PageTitleView pageTitle;
    @BindView(R.id.tv_receiver_people)
    TextView tvReceiverPeople;
    @BindView(R.id.tv_receiver_people_phone)
    TextView tvReceiverPeoplePhone;
    @BindView(R.id.tv_receiver_people_address)
    TextView tvReceiverPeopleAddress;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_bottom_back)
    TextView tvBottomBack;

    private String totalPrice;
    private String name;
    private String contact;
    private String address;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_back_paid_success;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        pageTitle = new PageTitleView((RelativeLayout) findViewById(R.id.rl_page_title), "支付成功", true);
        totalPrice = (String) SharedPreferencesUtils.getParam(BackPaidSuccessActivity.this, Constants.SHARED_PREFERENCES_KEY_RECEIVER_TOTALPRICE, "");
        name = (String) SharedPreferencesUtils.getParam(BackPaidSuccessActivity.this, Constants.SHARED_PREFERENCES_KEY_RECEIVER_NAME, "");
        contact = (String) SharedPreferencesUtils.getParam(BackPaidSuccessActivity.this, Constants.SHARED_PREFERENCES_KEY_RECEIVER_CONTACT, "");
        address = (String) SharedPreferencesUtils.getParam(BackPaidSuccessActivity.this, Constants.SHARED_PREFERENCES_KEY_RECEIVER_ADDRESS, "");
        tvReceiverPeople.setText("收货人: " + name);
        tvReceiverPeoplePhone.setText(contact);
        tvReceiverPeopleAddress.setText(address);
        String price = "总价: <font color='#CE0537'>" + "￥" + totalPrice + "</font>";
        tvTotalPrice.setText(Html.fromHtml(price));
        pageTitle.getLeftButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backIntent();
            }
        });
    }

    @Override
    protected void updateViews() {

    }


    @OnClick(R.id.tv_bottom_back)
    public void bottomBack(View view) {
        backIntent();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backIntent();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void backIntent() {
        if (Constants.IS_START_ORDER_ACTIVITY) {
            Intent intent = new Intent();
            intent.setAction(OrderActivity.orderAction);
            intent.putExtra("currentId", 0);
            sendBroadcast(intent);
        } else {
            Intent intent2 = new Intent(BackPaidSuccessActivity.this, OrderActivity.class);
            intent2.putExtra("currentId", 0);
            startActivity(intent2);
        }
        RxBus.getDefault().post(Constants.ORDER_PAYMENT_SECCESS_CODE, new CommonEvent("订单付款成功"));
        finish();
    }
}
